package com.eol.admin.modules.business.content;

import com.eol.admin.modules.logging.log.Log;
import com.eol.common.constants.BaseCommonConstants;
import com.eol.entity.po.content.ZLabel;
import com.eol.mapper.config.tkMybatis.TableData;
import com.eol.service.content.ZLabelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liulinhui
 * Date: 2020/5/31
 * Time: 10:51 AM
 * Description: LabelController
 */
@RestController
@RequestMapping("api/label")
public class LabelController {


    @Resource
    private ZLabelService zLabelService;


    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResponseEntity<Object> getLabels(
            @RequestParam(value = "page", defaultValue = BaseCommonConstants.PAGE_HELP_PAGE) Integer page,
            @RequestParam(value = "size", defaultValue = BaseCommonConstants.PAGE_HELP_LIMIT) Integer limit,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam("type") Integer type) {
        PageHelper.startPage(page + 1, limit);
        List<ZLabel> list = zLabelService.selectByQuery(ZLabel.builder()
                .type(type)
                .name(name)
                .build());
        PageInfo<ZLabel> pageInfo = new PageInfo<>(list);
        TableData<ZLabel> tableData = new TableData<>(pageInfo.getTotal(), list);
        PageHelper.clearPage();
        return new ResponseEntity<>(tableData, HttpStatus.OK);
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    @Log("添加标签")
    public ResponseEntity<Object> add(
            @RequestParam(value = "name") String name,
            @RequestParam("type") Integer type) {
        ZLabel zLabel = zLabelService.selectOne(ZLabel.builder().name(name).type(type).build());
        if (null == zLabel) zLabelService.insertSelective(ZLabel.builder()
                .name(name)
                .type(type)
                .createTime(new Date())
                .build());
        return new ResponseEntity<>(null, HttpStatus.OK);
    }

}
